/**
 * 此文件需要兼容在node环境下运行
 */

type ProxyList = [string, string][]

function isNode(): boolean {
  return typeof process !== 'undefined' && !!process.versions?.node
}

function isDevMode(): boolean {
  // return import.meta.env.DEV
  return process.env.NODE_ENV === 'development'
}

// function isProdMode(): boolean {
//   return import.meta.env.PROD
// }

// 开发环境
function getDevLoginApiUrl() {
  // const url = 'http://jwe3.jwesystem.com:8199/#/login'
  // const url = 'http://xffs.jwesystem.com:8199/#/login'
  // 大生
  const url = 'http://dszhcj.jwesystem.com:8199/#/login'
  // const url = 'http://dsszhcj.jwesystem.com:8199/#/login'
  // const url = 'http://dsjmf.jwesystem.com:8199'
  // const url = 'http://dsxek.jwesystem.com:8199/#/login'
  // const url = 'http://fjjy.jwesystem.com:8199/#/login'
  // 可以当测试环境用
  // const url = 'http://msszh.jwesystem.com:8199/#/login'
  // const url = 'http://ssfz.jwesystem.com:8199'
  // const url = 'http://fjcy9.jwesystem.com:8199'
  // const url = 'http://tdfz.jwesystem.com:8199/#/login'
  // const url = 'http://wxsfzx.jwesystem.com:8199/#/login'
  // const url = 'http://ydhfz2.jwesystem.com:8199/#/login'
  // const url = 'http://ydhfz1.jwesystem.com:8199/#/login'
  // const url = 'http://xjhy.jwesystem.com:8199/#/login'
  // const url = 'http://wxym.jwesystem.com:8199/#/login'
  // const url = 'http://dsjmf.jwesystem.com:8199'
  // const url = 'http://gqzhcj.jwesystem.com:8199'
  // const url = 'http://schq2.jwesystem.com:8199'
  // const url = 'http://dsszhcj.jwesystem.com:8199'
  // const url = 'http://dsxxfcj.jwesystem.com:8199'
  // 有区域 测试后需要还原数据
  // const url = 'http://btzhcj.jwesystem.com:8199'
  // const url = 'http://hjxyzhcj.jwesystem.com:8199'

  //   const url = window.location.href
  const str = url.match(/http:(\S*).com/)?.[1]
  if (str) {
    const h = str.split('.')
    if (h.length === 1) {
      return 'http://jwefs.com:55020'
    } else if (h[0] === '//jingweifangji') {
      return 'http://tdfz.jwefs.com:55020'
    } else if (h[0] === '//jwsf') {
      return 'http://tdfz.jwefs.com:55020'
    } else if (h[0] === '//wxsfzx') {
      return 'http://dszhcj.jwefs.com:55020'
    } else if (h[0] === '//tkxfz') {
      return 'http://tdfz.jwefs.com:55020'
    } else {
      return 'http:' + h[0] + '.jwefs.com' + ':55020'
    }
  } else {
    console.error('未匹配到域名')
  }
}

// 生产环境
function getProdLoginApiUrl() {
  // 避免构建时使用window
  if (isNode()) return
  const url = window.location.href
  // const url = 'http://dszhcj.jwefs.com:55020'
  const str = url.match(/http:(\S*).com/)?.[1]
  if (str) {
    const h = str.split('.')
    if (h.length === 1) {
      return 'http://jwe3.jwefs.com:55020'
    } else if (h[0] === '//jingweifangji') {
      return 'http://tdfz.jwefs.com:55020'
    } else if (h[0] === '//jwsf') {
      return 'http://tdfz.jwefs.com:55020'
    } else if (h[0] === '//wxsfzx') {
      return 'http://dszhcj.jwefs.com:55020'
    } else if (h[0] === '//tkxfz') {
      return 'http://tdfz.jwefs.com:55020'
    } else {
      return 'http:' + h[0] + '.jwefs.com' + ':55020'
    }
  } else {
    console.error('未匹配到域名')
  }
}

// axios baseURL
export const BASE_API_URL = isDevMode() ? '/api' : 'http://e3pgatetest.jwesystem.com:55000'

// 登录用
export const LOGIN_API_URL = isDevMode() ? '/loginApi' : getProdLoginApiUrl()

// 代理映射
export const PROXY_LIST: ProxyList = [
  ['/api', 'http://e3pgatetest.jwesystem.com:55000'],
  ['/loginApi', getDevLoginApiUrl()!]
]

// // vite proxy
// export const PROXY = createProxy(PROXY_LIST)
